package com.runchinaup.runchilibrary.upg.rcu;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

/**
 * Created by nopointer on 2017/11/13.
 * 3段式的版本
 */


public class LocalVersionInfo_3 extends LocalVersionInfo_2 {

    public int number = 0;

    public String getVersionStr(LocalVersionInfo_3 appVersion) {
        return String.format("%d.%d.%d", appVersion.major, appVersion.minor, appVersion.number);
    }

    public String getVersionStr() {
        return getVersionStr(this);
    }

    public static LocalVersionInfo_3 parser(String versionStr) {
        LocalVersionInfo_3 result = new LocalVersionInfo_3();
        String tmp[] = versionStr.split("\\.");
        result.major = Integer.valueOf(tmp[0]);
        result.minor = Integer.valueOf(tmp[1]);
        result.number = Integer.valueOf(tmp[2]);
        return result;
    }

    public boolean enableUpdate(LocalVersionInfo_3 local, LocalVersionInfo_3 remote) {
        if (remote.major > local.major) {//major>major
            return true;
        } else if (remote.major == local.major) {//major==major
            if (remote.minor > local.minor) {
                return true;
            } else if (remote.minor == local.minor) {
                if (remote.number > local.number) {
                    return true;
                }
            }
        }
        return false;
    }

    public static String getVersionName(Context context) {
        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            String version = info.versionName;
            return version;
        } catch (Exception e) {
            e.printStackTrace();
            return "1.0.0";
        }
    }

}
